package com.yz.pay.controller;

import com.yz.api.facade.pay.PayFacade;
import com.yz.model.dto.pay.AlipayNotifyDto;
import com.yz.model.dto.order.PayOrderRequest;
import com.yz.common.resp.BaseResponse;
import com.yz.common.utils.ResultUtils;
import com.yz.model.vo.pay.PayVo;
import com.yz.pay.properties.ReturnProperties;
import com.yz.pay.service.PayService;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created With IntelliJ IDEA.
 * Descriptions:
 * <p>
 * User:Mr.Du
 * Date:2024/3/14
 * Time:15:32
 */
@Slf4j
@RestController
@RequiredArgsConstructor
public class PayController implements PayFacade {

    private final PayService payService;

    private final ReturnProperties returnProperties;

    @Override
    public BaseResponse<String> reCharge(Long uid, PayOrderRequest payOrderRequest) {
        if(payOrderRequest == null || uid == null){
            throw new RuntimeException("参数不能为空");
        }
        String res = payService.reCharge(uid, payOrderRequest);
        return ResultUtils.success("",res);
    }

    @Override
    public String notify(AlipayNotifyDto alipayNotifyDto) {
        if(alipayNotifyDto == null){
            throw new RuntimeException("参数不能为空");
        }
        String res = payService.notifyUrl(alipayNotifyDto);
        return res;
    }

    @Override
    public void finish(HttpServletResponse response) {
        try{
            response.sendRedirect(returnProperties.getUrl());
        } catch (Exception e){
            log.error("【同步跳转，告诉你是否调用成功，不能拿来判断支付成功】",e);
        }

    }

    @Override
    public BaseResponse<List<PayVo>> getPayVoList() {
        List<PayVo> payVoList = payService.getPayVoList();
        return ResultUtils.success(payVoList);
    }
}
